<template>
  <div class="container">
    <el-row class="mb10">
      <el-col :span="16">
        <div class="flex">
          <el-input
            clearable
            v-model="data.searchParams.projectName"
            placeholder="项目名称"
            style="width: 130px; margin-right: 15px"
          >
          </el-input>
          <SelectOption
            :options="options.projectnameList"
            :placeholder="'申报类型'"
            @change-value="changeValue"
          >
          </SelectOption>
          <SelectOption
            :options="options.unitnameList"
            :placeholder="'申报奖项'"
            @change-value="changeValue1"
          >
          </SelectOption>

          <el-date-picker
            style="width: 233px; margin-right: 15px"
            v-model="data.time"
            type="daterange"
            range-separator="-"
            start-placeholder="申报开始日期"
            end-placeholder="申报结束日期"
            @change="dataPickerChange"
          >
          </el-date-picker>
          <el-button class="ordinary" @click="search()">搜索</el-button>
        </div>
      </el-col>
      <el-col :span="8" align="right">
        <el-button class="ordinary" @click="getExport()" v-limit="'KJSL:PTJCJS:KJZYZHPT:EXPERT'"
          >批量导出</el-button
        >
        <el-button type="danger" @click="deleteList" v-limit="'KJSL:PTJCJS:KJZYZHPT:DELETE'"
          >批量删除</el-button
        >
        <el-button type="primary" @click="add" v-limit="'KJSL:KJJL:KJCGJL:REWARDS'"
          >申报奖励</el-button
        >
      </el-col>
    </el-row>

    <div>
      <Table
        :loading="data.isLoading"
        :tableArr="data.allTableData"
        :pagination="data.page"
        :columns="data.columns"
        :total="data.page.total"
        @delectTable="delectIds"
        @currentChange="currentChange"
        @sizeChange="sizeChange"
      >
        <template #operate="{ row }">
          <div v-if="row.status == '待提交'">
            <el-button
              size="small"
              class="ordinary"
              @click="handleEdit(row)"
              v-show="row.updateFlag"
              v-limit="'KJSL:KJJL:KJCGJL:EDIT'"
              >编辑</el-button
            >
            <el-button size="small" class="ordinary" @click="handleDetail(row)">详情</el-button>
            <el-button
              size="small"
              class="ordinary"
              @click="handletj(row)"
              v-limit="'KJSL:KJJL:KJCGJL:SUBMIT'"
              >提交</el-button
            >
          </div>
          <div v-if="row.status == '待形式审查'||row.status == 1">
            <el-button
              v-if="row.currUserId?.split(',').includes(data.username)"
              size="small"
              class="ordinary"
              @click="handlesh(row)"
              v-limit="'KJSL:KJJL:KJCGJL:EXAMINE'"
              >形式审查</el-button
            >
            <el-button size="small" class="ordinary" @click="handleDetail(row)">详情</el-button>
          </div>
          <div v-if="row.status == '待奖金核算'">
            <el-button
              v-if="row.currUserId?.split(',').includes(data.username)"
              size="small"
              class="ordinary"
              @click="handlesh(row)"
              v-limit="'KJSL:KJJL:KJCGJL:EXAMINE'"
              >奖金核算</el-button
            >
            <el-button size="small" class="ordinary" @click="handleDetail(row)">详情</el-button>
          </div>
          <div v-if="row.status == '待公示'">
            <el-button
              v-if="row.currUserId?.split(',').includes(data.username)"
              size="small"
              class="ordinary"
              @click="handlesh(row)"
              v-limit="'KJSL:KJJL:KJCGJL:EXAMINE'"
              >公示</el-button
            >
            <el-button size="small" class="ordinary" @click="handleDetail(row)">详情</el-button>
          </div>
          <div v-if="row.status == '已公示'">
            <el-button
              size="small"
              class="ordinary"
              @click="handlecx(row)"
              v-limit="'KJSL:KJJL:KJCGJL:REVOKE'"
              >撤销</el-button
            >
            <el-button size="small" class="ordinary" @click="handleDetail(row)">详情</el-button>
          </div>
          <div v-if="row.status == '已驳回'">
            <el-button
              size="small"
              class="ordinary"
              @click="handleEdit(row)"
              v-limit="'KJSL:KJJL:KJCGJL:EDIT'"
              >编辑</el-button
            >
            <el-button size="small" class="ordinary" @click="handleCancel(row)">作废</el-button>
            <el-button
              size="small"
              class="ordinary"
              @click="handletj(row)"
              v-limit="'KJSL:KJJL:KJCGJL:SUBMIT'"
              >提交</el-button
            >
          </div>
          <div v-if="row.status == '已作废'">
            <el-button size="small" class="ordinary" @click="handleDetail(row)">详情</el-button>
          </div>
        </template>
      </Table>
    </div>
    <el-dialog
      v-model="data.detailVisible"
      width="900px"
      :show-close="false"
      :close-on-click-modal="false"
      @closed="close"
      destroy-on-close
    >
      <template #title>
        <div class="my-header" v-loading="data.detailVisible_loading">
          <el-tabs v-model="activeName" class="demo-tabs">
            <el-tab-pane
              label="流程图"
              name="four"
              v-if="(data.status == 1 || data.status == 0) && data.currentRow.status != '待提交'&&data.detailVisible"
            >
              <flowChart :processInstanceId="data.processInstanceIdtwo"></flowChart>
            </el-tab-pane>
            <el-tab-pane label="基本信息" name="first">
              <div v-if="data.detailVisible">
                <Information
                  ref="getAssociation"
                  @valueFlag="valueFlag1"
                  :addFlag="-1"
                  :currentStatus="data.currentRow.status"
                  @awardType="awardType"
                  :processInstanceId="data.processInstanceIdtwo"  
                  :orgAllList="data.orgAllList"
                >
                </Information>
              </div>
            </el-tab-pane>
            <el-tab-pane label="附表信息" name="middle">
              <div v-if="data.awardSelect.sbjx == 1">
                <div v-if="data.awardSelect.sblx == 1">
                  <Schedulexq
                    ref="getMiddle"
                    @valueFlag2="valueFlag2"
                    :fbData="data.awardSelect.pants"
                    :users="data.users"
                    :addFlag="data.addFlag"
                    @usersChange="usersChange"
                    @paramsChange="paramsChange6"
                  ></Schedulexq>
                </div>
                <div v-else-if="data.awardSelect.sblx == 2">
                  <fmzlxq
                    ref="getMiddle"
                    @valueFlag2="valueFlag2"
                    :fbData="data.awardSelect.patents"
                    :zlData="data.zlData"
                    :addFlag="data.addFlag"
                    @paramsChange="paramsChange22"
                  ></fmzlxq>
                </div>
                <div v-else-if="data.awardSelect.sblx == 3">
                  <jsjrjzzxq
                    ref="getMiddle"
                    @valueFlag2="valueFlag2"
                    :fbData="data.awardSelect.softwarecopyrights"
                    :addFlag="data.addFlag"
                    @paramsChange="paramsChange2"
                  ></jsjrjzzxq>
                </div>
                <div v-else-if="data.awardSelect.sblx == 4">
                  <jsbzxq
                    ref="getMiddle"
                    @valueFlag2="valueFlag2"
                    :fbData="data.awardSelect.technicalnorms"
                    :addFlag="data.addFlag"
                    @paramsChange="paramsChange1"
                  >
                  </jsbzxq>
                </div>
                <div v-else-if="data.awardSelect.sblx == 5">
                  <jsgfxq
                    ref="getMiddle"
                    @valueFlag2="valueFlag2"
                    :fbData="data.awardSelect.technicalmethods"
                    :addFlag="data.addFlag"
                    @paramsChange="paramsChange"
                  >
                  </jsgfxq>
                </div>
                <div v-else-if="data.awardSelect.sblx == 6">
                  <xslzxq
                    ref="getMiddle"
                    @valueFlag2="valueFlag2"
                    :fbData="data.awardSelect.composition"
                    :orgAllList="data.orgAllList"
                    :addFlag="data.addFlag"
                    @paramsChange="paramsChange5"
                  >
                  </xslzxq>
                </div>
                <div v-else-if="data.awardSelect.sblx == 7">
                  <xslwxq
                    ref="getMiddle"
                    @valueFlag2="valueFlag2"
                    :fbData="data.awardSelect.thesis"
                    :addFlag="data.addFlag"
                    :orgAllList="data.orgAllList"
                    @paramsChange="paramsChange4"
                  ></xslwxq>
                </div>
              </div>
              <div v-else-if="data.awardSelect.sbjx == 2">
                <zdkjxmlxjxq
                  ref="getMiddle"
                  @valueFlag2="valueFlag2"
                  :fbData="data.awardSelect.projects"
                  :users="data.users"
                  :addFlag="data.addFlag"
                  @usersChange="usersChange"
                  @paramsChange="paramsChange8"
                >
                </zdkjxmlxjxq>
              </div>
              <div v-else>
                <cxptjsjxq
                  ref="getMiddle"
                  @valueFlag2="valueFlag2"
                  :fbData="data.awardSelect.innovate"
                  :users="data.users"
                  :addFlag="data.addFlag"
                  @usersChange="usersChange"
                  @paramsChange="paramsChange9"
                ></cxptjsjxq>
              </div>
            </el-tab-pane>
            <el-tab-pane label="附件信息" name="second">
              <div class="apply">
                <h3>科技奖励申请表</h3> 
                <el-upload
                  disabled
                  ref="uploadRef"
                  :limit="1"
                  :headers="headers"
                  v-model:file-list="data.applicationList"
                  action="/fastDfs/file/upload"
                  :on-success="handleAttachment"
                  :on-preview="previewAttachment"
                  :on-remove="removeAttachment"
                  :on-exceed="handleExceed"
                  :on-error="handleError"
                >
                  <el-button type="primary">点击上传</el-button>
                </el-upload>
              </div>
              <hr>
              <platformFilesxq
                :files="data.fileList"
                title="质证材料"
                @dataChange="dataChange"
                @filesParams="filesParams"
                download="KJSL:KJJL:KJCGJL:DOWNLOAD"
                upload="KJSL:KJJL:KJCGJL:UPLOAD"
                fileDelete="KJSL:KJJL:KJCGJL:FILEDELETE"
              ></platformFilesxq>
            </el-tab-pane>
            <el-tab-pane label="变更记录" name="third" v-if="data.addFlag == 1">
              <record :changeLogs="data.changeLogs"></record>
            </el-tab-pane>
          </el-tabs>
        </div>
      </template>

      <template #footer>
        <div class="dialog-footer" style="float: left" v-if="data.status == 1">
          <el-form label-width="100px" :rules="rules" ref="ruleFormRef" :model="ruleForm">
            <el-form-item label="审批意见" prop="comment">
              <el-input
                :rows="2"
                type="textarea"
                placeholder="审批意见"
                v-model="ruleForm.comment"
                style="width: 450px"
                clearable
              />
            </el-form-item>
          </el-form>
        </div>
        <div class="dialog-footer" v-if="data.status == 0">
          <el-button @click="handleClose">关闭</el-button>
          <!--          <el-button type="primary" @click="sure">保存</el-button>-->
        </div>
        <div class="dialog-footer" v-if="data.status == 1" style="margin-top: 20px">
          <el-button @click="handleClose">关闭</el-button>
          <el-button type="danger" @click="handlebh">驳回</el-button>
          <el-button type="primary" @click="handletg">通过</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog
      v-model="data.dialogVisible"
      width="900px"
      :show-close="false"
      :close-on-click-modal="false"
      @closed="close"
      destroy-on-close
    >
      <template #title>
        <div class="my-header" v-loading="data.detail_loading">
          <el-tabs v-model="activeName" class="demo-tabs">
            <el-tab-pane label="流程图" name="four" v-if="( data.status == 0) && data.currentRow.status != '待提交'&&data.dialogVisible">
              <flowChart :processInstanceId="data.processInstanceIdtwo"></flowChart>
            </el-tab-pane>

            <el-tab-pane label="基本信息" name="first">
              <div v-if="data.dialogVisible">
                <Information
                  ref="getAssociation"
                  @valueFlag="valueFlag1"
                  :addFlag="data.addFlag"
                  :currentStatus="data.currentRow.status"
                  @awardType="awardType"
                  :orgAllList="data.orgAllList"
                >
                </Information>
              </div>
            </el-tab-pane>
            <el-tab-pane label="附表信息" name="middle">
              <div v-if="data.awardSelect.sbjx == 1">
                <div v-if="data.awardSelect.sblx == 1">
                  <Schedule
                    ref="getMiddle"
                    @valueFlag2="valueFlag2"
                    :fbData="data.awardSelect.pants"
                    :users="data.users"
                    :addFlag="data.addFlag"
                    @usersChange="usersChange"
                    @paramsChange="paramsChange6"
                  ></Schedule>
                </div>
                <div v-else-if="data.awardSelect.sblx == 2">
                  <fmzl
                    ref="getMiddle"
                    @valueFlag2="valueFlag2"
                    :fbData="data.awardSelect.patents"
                    :zlData="data.zlData"
                    :addFlag="data.addFlag"
                    @paramsChange="paramsChange22"
                    :orgAllList="data.orgAllList"
                  ></fmzl>
                </div>
                <div v-else-if="data.awardSelect.sblx == 3">
                  <jsjrjzz
                    ref="getMiddle"
                    @valueFlag2="valueFlag2"
                    :fbData="data.awardSelect.softwarecopyrights"
                    :addFlag="data.addFlag"
                    @paramsChange="paramsChange2"
                    :orgAllList="data.orgAllList"
                  ></jsjrjzz>
                </div>
                <div v-else-if="data.awardSelect.sblx == 4">
                  <jsbz
                    ref="getMiddle"
                    @valueFlag2="valueFlag2"
                    :fbData="data.awardSelect.technicalnorms"
                    :addFlag="data.addFlag"
                    @paramsChange="paramsChange1"
                  >
                  </jsbz>
                </div>
                <div v-else-if="data.awardSelect.sblx == 5">
                  <jsgf
                    ref="getMiddle"
                    @valueFlag2="valueFlag2"
                    :fbData="data.awardSelect.technicalmethods"
                    :addFlag="data.addFlag"
                    @paramsChange="paramsChange"
                  >
                  </jsgf>
                </div>
                <div v-else-if="data.awardSelect.sblx == 6">
                  <xslz
                    ref="getMiddle"
                    @valueFlag2="valueFlag2"
                    :fbData="data.awardSelect.composition"
                    :orgAllList="data.orgAllList"
                    :addFlag="data.addFlag"
                    @paramsChange="paramsChange5"
                  >
                  </xslz>
                </div>
                <div v-else-if="data.awardSelect.sblx == 7">
                  <xslw
                    ref="getMiddle"
                    @valueFlag2="valueFlag2"
                    :fbData="data.awardSelect.thesis"
                    :addFlag="data.addFlag"
                    :orgAllList="data.orgAllList"
                    @paramsChange="paramsChange4"
                  ></xslw>
                </div>
              </div>
              <div v-else-if="data.awardSelect.sbjx == 2">
                <zdkjxmlxj
                  ref="getMiddle"
                  @valueFlag2="valueFlag2"
                  :fbData="data.awardSelect.projects"
                  :users="data.users"
                  :addFlag="data.addFlag"
                  @usersChange="usersChange"
                  @paramsChange="paramsChange8"
                >
                </zdkjxmlxj>
              </div>
              <div v-else>
                <cxptjsj
                  ref="getMiddle"
                  @valueFlag2="valueFlag2"
                  :fbData="data.awardSelect.innovate"
                  :users="data.users"
                  :addFlag="data.addFlag"
                  @usersChange="usersChange"
                  @paramsChange="paramsChange9"
                ></cxptjsj>
              </div>
            </el-tab-pane>
            <el-tab-pane label="附件信息" name="second">
              <div class="apply">
                <h3>科技奖励申请表</h3> 
                <el-upload
                  ref="uploadRef"
                  :limit="1"
                  :headers="headers"
                  v-model:file-list="data.applicationList"
                  action="/fastDfs/file/upload"
                  :on-success="handleAttachment"
                  :on-preview="previewAttachment"
                  :on-remove="removeAttachment"
                  :on-exceed="handleExceed"
                  :on-error="handleError"
                >
                  <el-button type="primary">点击上传</el-button>
                </el-upload>
              </div>
              <hr>
              <platformFiles
                :files="data.fileList"
                title="质证材料"
                @dataChange="dataChange"
                @filesParams="filesParams"
                download="KJSL:KJJL:KJCGJL:DOWNLOAD"
                upload="KJSL:KJJL:KJCGJL:UPLOAD"
                fileDelete="KJSL:KJJL:KJCGJL:FILEDELETE"
              ></platformFiles>
            </el-tab-pane>
            <el-tab-pane label="变更记录" name="third" v-if="data.addFlag == 1">
              <record :changeLogs="data.changeLogs"></record>
            </el-tab-pane>
          </el-tabs>
        </div>
      </template>

      <template #footer>
        <div class="dialog-footer" style="float: left" v-if="data.status == 1">
          <el-form label-width="100px" :rules="rules" ref="ruleFormRef" :model="ruleForm">
            <el-form-item label="审批意见" prop="comment">
              <el-input
                :rows="2"
                type="textarea"
                placeholder="审批意见"
                v-model="ruleForm.comment"
                style="width: 450px"
                clearable
              />
            </el-form-item>
          </el-form>
        </div>
        <div class="dialog-footer" v-if="data.status == 0">
          <el-button @click="updatemoban"  v-if="data.addFlag == 1">下载模版</el-button>
          <el-button @click="close">关闭</el-button>
          <el-button type="primary" @click="sure">保存</el-button>
        </div>
        <div class="dialog-footer" v-if="data.status == 1" style="margin-top: 20px">
          <el-button @click="closeVisable">关闭</el-button>
          <el-button type="danger" @click="handlebh">驳回</el-button>
          <el-button type="primary" @click="handletg">通过</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog
      v-model="data.dealVisible"
      title="系统正在处理中....."
      width="40%"
      top="14%"
      height="400"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-alert
        type="success"
        :closable="false"
        style="margin-bottom: 20px"
        description="当前流程正在处理中,请稍等……"
      >
      </el-alert>
    </el-dialog>
  </div>
</template>
<script lang="js">
import flowChart from '@/components/PublicProcess/Chart/flowChart.vue'
import * as proxy from "@/constants/proxy"
import request from "@/utils/request"
import { downloadFile } from "@/utils/utils"
import Api from '@/views/science/api/awards'
import platformFiles from '@/views/science/components/platformFiles/platformFiles.vue'
import platformFilesxq from '@/views/science/components/platformFiles/platformFilesxq.vue'
import record from '@/views/science/components/record/record.vue'
import SelectOption from '@/views/science/components/selectOption/index.vue'
import Table from '@/views/science/components/table/table.vue'
import { Modals, tips } from '@/views/science/components/tips/tips'
import { ElMessage } from 'element-plus'
import { computed, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { servicesLoading } from '@/utils/loading'
import { type } from '@/utils/utils'

import cxptjsj from './cxptjsj.vue'
import cxptjsjxq from './cxptjsjxq.vue'
import fmzl from './fmzl.vue'
import fmzlxq from './fmzlxq.vue'
import Information from './information.vue'
import Informationxq from './informationxq.vue'
import jsbz from './jsbz.vue'
import jsbzxq from './jsbzxq.vue'
import jsgf from './jsgf.vue'
import jsgfxq from './jsgfxq.vue'
import jsjrjzz from './jsjrjzz.vue'
import jsjrjzzxq from './jsjrjzzxq.vue'
import Schedule from './schedule.vue'
import Schedulexq from './schedulexq.vue'
import xslw from './xslw.vue'
import xslwxq from './xslwxq.vue'
import xslz from './xslz.vue'
import xslzxq from './xslzxq.vue'
import zdkjxmlxj from './zdkjxmlxj.vue'
import zdkjxmlxjxq from './zdkjxmlxjxq.vue'
import { trace } from "@/components/PublicProcess/utils/api";


export default {
  components: {
    SelectOption,
    flowChart,
    Table,
    platformFiles,//附件
    platformFilesxq,//附件
    Information,//基本信息
    Informationxq,//基本信息
    Schedule,//科技奖项附表信息
    fmzl, //发明专利
    jsjrjzz, //计算机软件著作
    jsbz, //技术标准
    jsgf, //技术工法
    xslz, //学术论著
    xslw, //学术论文
    zdkjxmlxj, //重大科技项目立项奖
    cxptjsj, //创新平台建设奖
    Schedulexq,//科技奖项附表信息
    fmzlxq, //发明专利
    jsjrjzzxq, //计算机软件著作
    jsbzxq, //技术标准
    jsgfxq, //技术工法
    xslzxq, //学术论著
    xslwxq, //学术论文
    zdkjxmlxjxq, //重大科技项目立项奖
    cxptjsjxq, //创新平台建设奖
    record,//变更记录
  },
  setup() {
    const store = useStore()
    const startDate = ref(null);  // 开始时间绑定值
    const endDate = ref(null);  // 结束时间绑定值
    const activeName = ref('first')//tab切换
    const ruleForm = reactive({
      comment: '',
    })

    const rules = reactive({
      comment: [
        { required: true, message: '请输入审核意见', trigger: 'change', },
      ]
    })

    const ruleFormRef = ref()

    let data = reactive({
      orgAllList: [],
      times: null,
      taskId: '',
      businessKey: '',
      dealVisible: false,
      isLoading: false,
      detail_loading: false,
      constructionName: '',
      processInstanceId: '',
      changeLogs: [],//变更记录
      addFlag: 0,
      flag1: null,
      flag2: null,
      status: 0, //0编辑，1审核
      dialogVisible: false,//弹窗
      page: { page: 1, limit: 10, total: 0 },
      ids: [],
      time: [],
      searchParams: {
        projectName: '', // 项目名称
        applyClass: '',//申报类型
        awardApplyName: '', // 申报奖项
        startTime: '',
        endTime: '',
      },
      currentBonus:'',
      processInstanceIdtwo:'',
      bonus:'',
      allTableData: [],
      columns: [
/*        {
          key: 'projectNo',
          width: 140,
          label: '项目编号',
        },*/
        {
          key: 'relateName',
          width: 140,
          label: '名称',
        },
        {
          key: 'applyClass',
          width: 140,
          label: '申报类型',
        },
        {
          key: 'awardApplyName',
          width: 140,
          label: '申报奖项',
        },
        {
          key: 'applyUnitName',
          width: 140,
          label: '申报单位',
        },
        {
          key: 'applyTime',
          width: 140,
          label: '申报时间',
        },
        {
          key: 'applyUser',
          width: 140,
          label: '申报人',
        },
        {
          key: 'status',
          width: 140,
          label: '状态',
          textColor: true
        },
      ],
      pants: [],
      users: [],
      fileList: [], //附件信息

      awardSelect: {
        sbjx: 1,
        sblx: 1,
        pants: {}, //附表信息-科技创新成果奖-科学奖项
        patents: {}, //附表信息-科技创新成果奖-发明专利
        softwarecopyrights: {}, //科技创新成果奖-软件著作权
        technicalnorms: {}, //科技创新成果奖-技术标准
        technicalmethods: {}, //科技创新成果奖-技术工法
        composition: {}, //科技创新成果奖-学术著作
        thesis: {}, //科技创新成果奖-学术论文
        projects: {}, //重大科技项目立项奖
        innovate: {}, //创新平台建设奖
      },
      zlData: {},
      detailVisible_loading:false,
      detailVisible: false,
      currentRow:{},
      applicationList:[],
      username : JSON.parse(localStorage.getItem('userInfo')).username
    })
    //详情数据获取
    const detailVisibleData = async (id) => {
      data.detailVisible_loading = true
      const loading = servicesLoading({ fullscreen: true })

      Api.detailData({
        id: id
      })
      .then((res) => { // 获取列表数据
          loading.close()
          data.dialogVisible = true
          const resData = res.data.data
          store.commit('technologyStrength/getachievementINFO', { ...resData.baseInfo })
          // store.commit('technologyStrength/getfbxxINFO', { ...resData.pants })
          data.users = res.data.data.users
          data.fileList = res.data.data.fileList
          setTimeout(() => {
            data.applicationList = getFileList()
          }, 100);
          data.changeLogs = res.data.data.changeLogs
          data.processInstanceIdtwo = resData.baseInfo.processInstanceId
          trace(data.processInstanceIdtwo).then((res)=>{
            data.currentBonus = res.data?.length
            console.log(res.data,data.currentBonus,'trace');
          })
          data.taskId = resData.baseInfo.currTaskTd
          data.softwarecopyrights = resData.softwarecopyrights
          data.awardSelect.pants = resData.pants
          //专利
          data.awardSelect.patents = resData.patents

          data.awardSelect.sbjx = resData.baseInfo.awardApplyId
          data.awardSelect.sblx = resData.baseInfo.applyClassId

          Object.assign(data.awardSelect, resData);
          // console.log('详情数据获取', data.awardSelect.projects);
          // data.addFlag = 1
          store.commit('technologyStrength/getAddFlag', 1)
          ruleForm.comment = ''
          data.addFlag = 0
          data.detailVisible_loading = false
          // console.log('详情数据获取',resData);
        }).catch((error) => {
          loading.close()
          data.detailVisible_loading = false
        })


    }
    const handleDetail = (row) => {
      data.currentRow = row
      data.addFlag = 0

      detailVisibleData(row.id)
      store.commit('technologyStrength/getRowId', row.id)
      data.status = 0
      activeName.value = 'first'
    }

    const handleClose = () => {
      data.detailVisible = false
      data.recordShow = false;
      activeName.value = 'first'
    }
    const loading = ref(true)//加载
    // 存储下拉框数组的对象
    const options = reactive({
      projectnameList: computed(() => store.state.technologyStrength.KJSL_DECLARATION_TYPE),
      unitnameList: computed(() => store.state.technologyStrength.KJSL_APPLYING_FOR_AWARDS1),
    })
    const changeValue = (item) => data.searchParams.applyClass = item.itemValue
    const changeValue1 = (item) => data.searchParams.awardApplyName = item.itemValue
    // 清除日期选择器,删除的时候将日期清空
    const dataPickerChange = () => {
      if (data.time) {
        data.searchParams.startTime = data.time[0];
        data.searchParams.endTime = data.time[1];
      } else {
        data.searchParams.startTime = null;
        data.searchParams.endTime = null;
        data.time = [];
      }
    };

    data.searchParams.page = data.page.page
    data.searchParams.pageSize = data.page.limit

    const getListData = () => { // 获取列表数据
      loading.value = true   // 加载
      data.isLoading = true
      Api.getTotalList(data.searchParams)
        .then((res) => { // 获取列表数据
          let resData = res.data.data // 获取列表数据
          resData.records.map((item) => {
            if (item.status == -1) {
              item.status = "已作废"
            }
            if (item.status == 0) {
              item.status = "待提交"
            } else if (item.status == 1) {
              if(item.remark2==0){
                item.status = '待形式审查'
              }else if(item.remark2==1){
                item.status = '待奖金核算'
              }else if(item.remark2==2){
                item.status = '待公示'
              }
            } else if (item.status == 2) {
              // item.status = "正常办结"
              item.status = "已公示"
            } else if (item.status == 3) {
              item.status = "已退回"
            }
            else if (item.status == 4) {
              item.status = "已撤办"
            }
            else if (item.status == 5) {
              item.status = "强制办结"
            }
            else if (item.status == 6) {
              item.status = "已废弃"
            }
            else if (item.status == 7) {
              item.status = "已挂起"
            }
            else if (item.status == 8) {
              item.status = "已驳回"
            }

            item.applyTime = new Date(item.applyTime).getFullYear() + `-` + (new Date(item.applyTime).getMonth() + 1) + `-` + new Date(item.applyTime).getDate()
          })
          data.allTableData = resData.records   // 获取列表数据
          data.page.total = resData.total   // 获取列表数据
          data.isLoading = false
        })
        .finally(() => { // 获取列表数据
          loading.value = false  // 获取列表数据
        }).catch((error) => {
          data.isLoading = false
        })
    }

    const closeVisable = () => {

      ruleFormRef.value.resetFields();
      data.dialogVisible = false



    }

    //详情数据获取
    const detailData = async (id) => {
      data.detail_loading = true
      const loading = servicesLoading({ fullscreen: true })

      Api.detailData({
        id: id
      })
        .then((res) => { // 获取列表数据
          data.dialogVisible = true
          loading.close()

          const resData = res.data.data
          store.commit('technologyStrength/getachievementINFO', { ...resData.baseInfo })
          // store.commit('technologyStrength/getfbxxINFO', { ...resData.pants })
          data.users = res.data.data.users
          setTimeout(() => {
            data.applicationList = getFileList()
          }, 100);
          data.fileList = res.data.data.fileList
          data.changeLogs = res.data.data.changeLogs
          data.processInstanceId = resData.baseInfo.processInstanceId
          data.taskId = resData.baseInfo.currTaskTd
          data.softwarecopyrights = resData.softwarecopyrights
          data.awardSelect.pants = resData.pants
          //专利
          data.awardSelect.patents = resData.patents

          data.awardSelect.sbjx = resData.baseInfo.awardApplyId
          data.awardSelect.sblx = resData.baseInfo.applyClassId

          Object.assign(data.awardSelect, resData);
          // console.log('详情数据获取', data.awardSelect.projects);
          // data.addFlag = 1
          store.commit('technologyStrength/getAddFlag', 1)
          ruleForm.comment = ''
          // data.addFlag = 1
          data.detail_loading = false
          // console.log('详情数据获取',resData);
        }).catch((error) => {
          loading.close()

          data.detail_loading = false
        })
    }

    // 点击查询按键，分页设置为当前页为1，
    const search = () => {
      data.page.page = 1
      getListData()
    }
    const achievementINFO = reactive(store.state.technologyStrength.achievementINFO) 
    const getFileList = () => {
      const achievementINFO = store.state.technologyStrength.achievementINFO
      console.log(achievementINFO,'achievementINFO.remark1')
      if (type(achievementINFO.remark1)) {
        let fileList = JSON.parse(achievementINFO.remark1)
        return [fileList]
      } else {
        return []
      }
    }
    const handleAttachment = (uploadFile) => {
      if (uploadFile.code == 200) {
        ElMessage({
          message: '上传成功',
          type: 'success',
        })
        console.log(uploadFile,'uploadFile');
        let obj = {
          name: uploadFile.data.fileName,
          url: uploadFile.data.path,
        }
        data.applicationList = [obj]
        store.state.technologyStrength.achievementINFO.remark1 = JSON.stringify(obj)
        console.log(store.state.technologyStrength.achievementINFO ,'achievementINFO.remark1');
      } else {
        ElMessage({
          message: '上传失败',
          type: 'error',
        })
      }
    }
    const previewAttachment = (uploadFile) => {
      console.log(uploadFile, 'uploadFile')
      window.open(uploadFile.url)

    }
    const removeAttachment = (file, uploadFiles) => {
      console.log(uploadFiles, 'uploadFiles')
      store.state.technologyStrength.achievementINFO.remark1 = ''
      data.applicationList = []
    }
    const handleExceed = () => {
      ElMessage({
        message: '仅能上传1份申请表',
        type: 'wraning',
      })
    }
    const handleError = () => {
      ElMessage({
        message: '上传失败',
        type: 'error',
      })
    }
    // 删除
    const deleteList = () => {
      if (data.ids.length) {
        let list = []
        data.allTableData.forEach(e => {
          if (data.ids.includes(e.id)) {
            list.push(e.status)
          }
        })
        let flag = list.every(item=>{
          return (item=='待提交'||item=='已驳回')
        })
        if (!flag) {
          tips('只允许删除待提交和已驳回状态的数据', 'warning')
        } else {
          Modals('是否删除选择内容', '提示', 'warning').then(async () => {
            // console.log('可以删除');
            let res = await Api.deleteList({
              id: data.ids + ''
            })
            if (res.data.success) {
              tips('删除成功', 'success')
              getListData()
            }
            
          }).catch(() => {
            console.log("点击了取消")
          })
        }
      } else {
        tips('请选择', 'warning')
      }
    }

    const valueFlag1 = (flag) => {
      data.flag1 = flag
    }
    const valueFlag2 = (flag) => {
      data.flag2 = flag
    }



    const awardType = (awardSelect) => {
      Object.assign(data.awardSelect, awardSelect)
      Object.assign(data.zlData, awardSelect)
      console.log('关联对象切换', awardSelect)
    }

    const paramsChange = (params) => {
      console.log(params)
      data.awardSelect.technicalmethods = params
    }

    const paramsChange1 = (params) => {
      console.log(params)
      data.awardSelect.technicalnorms = params
    }

    const paramsChange2 = (params) => {
      // console.log(params)
      data.awardSelect.softwarecopyrights = params
    }
    //专利
    const paramsChange22 = (params) => {
      // console.log(params)
      data.awardSelect.patents = params
    }

    const paramsChange4 = (params) => {
      console.log(params)
      data.awardSelect.thesis = params
    }

    const paramsChange5 = (params) => {
      console.log(params)
      data.awardSelect.composition = params
    }

    const paramsChange6 = (params) => {
      data.awardSelect.pants = params
    }

    const paramsChange8 = (params) => {
      data.awardSelect.projects = params
    }

    const paramsChange9 = (params) => {
      console.log(params)
      data.awardSelect.innovate = params
    }

    const delectIds = (ids) => {
      data.ids = ids
    }

    const currentChange = (current) => {
      data.searchParams.page = current
      getListData()
    }

    const sizeChange = (size) => {
      console.log(size)
      data.searchParams.pageSize = size
      getListData()
    }

    const dataChange = () => {//附件
      console.log(1)
    }

    const filesParams = (params) => {
      data.fileList = params
    }

    const usersChange = (params) => {
      data.users = params
    }

    //修改
    const handleEdit = (row) => {
      store.commit('technologyStrength/getRowId', row.id)
      detailData(row.id)
      data.currentRow = row
      data.status = 0
      data.addFlag = 2
      activeName.value = 'first'
      console.log(11111,data.addFlag)
    }

    const handleCancel = (row)=>{
      console.log(row,'handleCancel')
      Modals("确认作废当前申请吗？", "提示", "warning")
      .then(async () => {
        Api.updatedateremark1({
          baseInfo: {
            id:row.id,
            status:-1
          }
        }).then(res=>{
          console.log(res.data,'Api.edit');
          getListData()
        })
      })
    }
    let handletj = (row) => {

      Modals("是否确认当前流程", "提示", "warning")
        .then(async () => {
          let res = await Api.startProcess({
            flowKey: 'tea1',
            businessKey: row.id,
            businessType: 'YW01',
            remark1: '0',
            random: (new Date()).valueOf()
          });


          if (res.data.code == 200) {

            data.dealVisible = true

            deal(row.id, 1)

          } else {

            tips(res.data.message, 'warning')

          }

        })
        .catch(() => { });

    }

    //审核
    const handlesh = (row) => {
      debugger
      const form = store.state.technologyStrength.achievementINFO
      activeName.value = 'four'
      store.commit('technologyStrength/getRowId', row.id)
      data.status = 1
      data.addFlag = 1
      data.businessKey = row.id
       data.processInstanceId=row.processInstanceId
       data.currentRow = row
      detailVisibleData(row.id)

    }

    //驳回
    const handlebh = () => {
      const form = store.state.technologyStrength.achievementINFO
      getAssociation.value.submitForm();
      ruleFormRef.value.validate(valid => {
        if (valid) {

          Modals('是否驳回本次内容', '提示', 'warning').then(async () => {
            data.detailVisible = false
            const loading = servicesLoading({ fullscreen: true })

            let res = await Api.refuseProcess({
              procInsId: data.processInstanceId,
              comment: ruleForm.comment,
              remark: form.bonus,
              businessKey: data.businessKey,
              businessType: 'YW01',
              random: (new Date()).valueOf()
            })
            loading.close()

            if (res.data.code == 200) {

              ruleFormRef.value.resetFields();
              data.dialogVisible = false
              data.dealVisible = true
              deal(data.businessKey, 8)

            } else {
              tips(res.data.message, 'warning')
            }

          }).catch(() => {
            console.log("点击了取消")
          })
        }
      })
    }

    let deal = (id, state) => {
      data.times = setInterval(() => {
        Api.detailData({
          id: id
        })
          .then((res) => {
            const resData = res.data.data
            if (resData.baseInfo.status == state) {
              data.dealVisible = false
              getListData()
              clearTimeout(data.times)
            }
          })
      }, 1000);
    }

    //通过
    const handletg = () => {
      const form = store.state.technologyStrength.achievementINFO
      // getAssociation.value.submitForm();
      if(form.bonus==''){
        tips('奖金不能为空', 'warning')
        return
      }

      ruleFormRef.value.validate(valid => {
        if (valid) {

          Modals('是否通过本次内容', '提示', 'warning').then(async () => {
            Api.updatedateremark1({
              baseInfo: form
            }).then(res=>{
              console.log(res.data,'Api.edit');
            })
            data.detailVisible = false
            const loading = servicesLoading({ fullscreen: true })

            let res = await Api.allowProcess({
              procInsId: data.processInstanceId,
              comment: ruleForm.comment,
              remark: form.bonus,
              remark1: data.currentBonus,
              businessKey: data.businessKey,
              businessType: 'YW01',
              random: (new Date()).valueOf()
            })
            loading.close()

            if (res.data.code == 200) {
              data.dialogVisible = false
              data.dealVisible = true
              dealCheck(data.businessKey)

            } else {

              tips(res.data.message, 'warning')

            }

          }).catch(() => {
            console.log("点击了取消")
          })
        }else{
          tips('请填写审核意见', 'warning')

        }
      })
    }



    let dealCheck = (id) => {
      data.times = setInterval(() => {
        Api.detailData({
          id: id
        })
          .then((res) => {
            let resData = res.data.data
            if (resData.baseInfo.status == 2) {
              data.dealVisible = false
              getListData()
              clearTimeout(data.times)
            }

            if (resData.baseInfo.status == 1 && resData.baseInfo.currTaskTd !== data.taskId) {
              data.dealVisible = false
              ruleFormRef.value.resetFields();
              getListData()
              clearTimeout(data.times)
            }

          })

      }, 1000);
    }

    const getExport = () => {
      let msg = '是否导出全部数据'
      if (data.ids.length > 0) {
        msg = '是否导出所选数据'
      }
      Modals(msg, '提示', 'warning').then(async () => {
        const params = {
          ...data.searchParams,
          ids: data.ids
        }
        await Api.export(params, "科技成果奖励")
      }).catch(() => {
        console.log("点击了取消")
      })
    }



    //撤销
    const handlecx = (row) => {
      Modals('是否撤销审核通过的内容', '提示', 'warning').then(async () => {
        let res = await Api.rollback({
          id: row.id
        })
        if (res.data.success) {
          tips('撤销成功', 'success')
          getListData()
        }
      }).catch(() => {
        console.log("点击了取消")
      })
    }

    const add = () => {
      store.state.technologyStrength.achievementINFO.projectYear = new Date().getFullYear()
      console.log(store.state.technologyStrength.achievementINFO.projectYear,'store.state.technologyStrength.achievementINFO.projectYear');
      store.commit('technologyStrength/getAddFlag', 0)
      data.addFlag = 1
      data.dialogVisible = true //弹窗
      data.status = 0
      data.currentRow.status = ''
      activeName.value = 'first'
      data.awardSelect.sbjx = 1
      data.awardSelect.sblx = 1
      store.commit('technologyStrength/emptyachievementINFO')
    }

    const close = () => {
      console.log('close');
      data.addFlag = 0
      data.applicationList=[]
      achievementINFO.remark1 = ''
      data.dialogVisible = false
      data.fileList = []
      data.awardSelect.sbjx = 0
      data.awardSelect.sblx = 0
      console.log(11111,data.addFlag)
      // ruleFormRef.value.resetFields()

    }
    const getAssociation = ref()
    const getMiddle = ref()

    const getUnit = () => {

      return request({
        url: proxy.CHECC_API_CMS + "/org/getSysOrgList",
        method: "get",
      }).then(res => {
        data.orgAllList = res.data.data
      })
    }

    const updatemoban = async () => { // 确定
      const form = store.state.technologyStrength.achievementINFO
      getAssociation.value.submitForm()
      const mingzitou = form.awardApplyName
      const mingziwei = form.applyClass
      /*      Modals('是否下载模版', '提示', 'warning').then(async () => {
              const form = store.state.technologyStrength.achievementINFO
              getAssociation.value.submitForm()
              const params = {
                id: form.id
              }

              const mingzitou = form.awardApplyName
              const mingziwei = form.applyClass
              await Api.updatemb(params,mingzitou + mingziwei)*/
        Modals('是否下载模版', '提示', 'warning')
            .then(async () => {
              loading.value = true;

              return request({
                url: proxy.CHECC_API_CMS + "/kgptKjslTawardTechresult/template",
                method: "post",
                responseType: "blob",
                data: {
                    id: form.id
                },
              })
                  .then((res) => {
                    const name = mingzitou+mingziwei+'.docx';
                    const url = URL.createObjectURL(res.data);
                    downloadFile(url, name);
                  })
                  .finally(() => {
                    loading.value = false;
                  });
            })
            .catch(() => {
              console.log("点击了取消");
            });
    };
    const sure = async () => { // 确定
      const form = store.state.technologyStrength.achievementINFO
      console.log(getAssociation.value,'getAssociation.value');
      getAssociation.value.submitForm()
      getMiddle.value.submitForm()

      if (!data.flag1) {
        ElMessage({
          message: '请将基本信息补充完整',
          type: 'warning',
        })
        return
      }
      if (!data.flag2) {
        ElMessage({
          message: '请将附表信息补充完整',
          type: 'warning',
        })
        return
      }

      let params;
      if (data.awardSelect.sbjx == 1) {
        if (data.awardSelect.sblx == 1) {
          params = {
            baseInfo: form,
            pants: data.awardSelect.pants,
            users: data.users,
            fileList: data.fileList
          }
        } else if (data.awardSelect.sblx == 2) {
          params = {
            baseInfo: form,
            patents: data.awardSelect.patents,
            fileList: data.fileList
          }
        } else if (data.awardSelect.sblx == 3) {
          params = {
            baseInfo: form,
            softwarecopyrights: data.awardSelect.softwarecopyrights,
            fileList: data.fileList
          }
        } else if (data.awardSelect.sblx == 4) {
          params = {
            baseInfo: form,
            technicalnorms: data.awardSelect.technicalnorms,
            fileList: data.fileList
          }
        } else if (data.awardSelect.sblx == 5) {
          params = {
            baseInfo: form,
            technicalmethods: data.awardSelect.technicalmethods,
            fileList: data.fileList
          }
        } else if (data.awardSelect.sblx == 6) {
          params = {
            baseInfo: form,
            composition: data.awardSelect.composition,
            fileList: data.fileList
          }
        } else if (data.awardSelect.sblx == 7) {
          params = {
            baseInfo: form,
            thesis: data.awardSelect.thesis,
            fileList: data.fileList
          }
        }
      } else if (data.awardSelect.sbjx == 2) {
        params = {
          baseInfo: form,
          projects: data.awardSelect.projects,
          users: data.users,
          fileList: data.fileList
        }
      } else if (data.awardSelect.sbjx == 3) {
        params = {
          baseInfo: form,
          innovate: data.awardSelect.innovate,
          fileList: data.fileList
        }
      }
      let res
      if (form.id) {
        res = await Api.edit(params)
      } else {
        res = await Api.insert(params)
      }
      if (res.data) {
        tips('保存成功', 'success')
        store.commit('technologyStrength/emptyachievementINFO')
        getListData()  // 获取列表数据
        activeName.value = 'first'
        data.dialogVisible = false  // 弹窗
      }
    }

    getListData()
    getUnit()
    const cookie = () => localStorage.getItem('token')


    return {
      headers: {
        Authorization: 'Bearer ' + cookie(),
      },
      dealCheck,
      delectIds,
      deal,
      changeValue,
      changeValue1,
      currentChange,
      sizeChange,
      search,
      filesParams,
      usersChange,

      valueFlag1,
      valueFlag2,
      awardType,
      paramsChange,
      paramsChange1,
      paramsChange2,
      paramsChange4,
      paramsChange5,
      paramsChange6,
      paramsChange8,
      paramsChange9,

      dataChange,
      getAssociation,
      activeName,
      options,
      data,
      ruleForm,
      rules,
      ruleFormRef,
      handleCancel,
      handleEdit,
      handletj,
      handlesh,
      handlecx,
      handlebh,
      handletg,
      closeVisable,
      // 列表
      getListData,
      // 新增
      add,
      sure,
      deleteList,
      handleAttachment,
      previewAttachment,
      removeAttachment,
      handleExceed,
      handleError,
      startDate,
      endDate,
      getMiddle,
      dataPickerChange,
      getExport,
      close,
      paramsChange22,
      handleDetail, //详情
      handleClose, //详情
      updatemoban,
    }
  },
}
</script>
<style lang="scss" scoped>
.container {
  background-color: white;
  padding: 20px;
}

/deep/.el-dialog > .el-dialog__footer {
  position: relative;
  top: -10px;
}

:global(.el-dialog__body) {
  padding: 0;
}

.el-col {
  line-height: 40px;
}

.el-form-item {
  .el-input {
    width: 100px;
    background-color: aliceblue;
  }

  .el-select {
    width: 100px;
  }
}

.custom-color {
  color: red !important;
  /* 确保优先级高于其他样式 */
}

.el-table th {
  background-color: #f0f0f0;
  /* 修改表头背景色 */
  color: #333;
  /* 修改表头文字颜色 */
}

/* .demo-tabs > .el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
} */

.open-title {
  font-size: 20px;
  font-weight: 700;
  text-align: center;
}

/deep/.el-dialog__body {
  display: block !important;
}

.mt15 {
  margin-top: 15px;
}

.mb10 {
  margin-bottom: 10px;
}

.tc {
  text-align: center;
}

.align-center {
  align-items: center;
}

.space-between {
  justify-content: space-between;
}

.mt20 {
  margin-top: 20px;
}

.flex {
  display: flex;
}
.apply{
  margin-bottom: 20px;
}
</style>
